#include "simulation.h"
#include "ui_simulation.h"

Simulation::Simulation(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Simulation)
{
    ui->setupUi(this);

    bdd = QSqlDatabase::addDatabase("QMYSQL");
    bdd.setHostName("juliendonuts.no-ip.org");
    bdd.setDatabaseName("domotiqueFoyer");
    bdd.setUserName("projetTER");
    bdd.setPassword("terdomo");

    if(!bdd.open())
    {
        qDebug() << bdd.lastError().text();
    }

    QSqlQuery recupPiece(bdd);

    if(recupPiece.exec("SELECT Nom_Piece, ID_Piece FROM Piece"))
    {
        while(recupPiece.next())
        {
            listePiece.append(new Piece(this, recupPiece.value(0).toString(), recupPiece.value(1).toInt(), bdd));
        }
    }
    else
    {
        qDebug() << recupPiece.lastError().text();
    }

    for(int i=0; i<listePiece.size(); ++i)
    {
        ui->affPiece->addTab(listePiece.at(i), listePiece.at(i)->getNom());
    }
}

Simulation::~Simulation()
{
    delete ui;
}

void Simulation::on_actualiser_clicked()
{
    for(int i=0; i<listePiece.size(); ++i)
    {
        listePiece.at(i)->MAJEquipement();
    }
}
